﻿using Newtonsoft.Json;
using System.Net;
using System.Text;
using System.Windows;
using UDITool;

namespace ZebraUDI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string startPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

        public MainWindow()
        {
            InitializeComponent();

            // 获取本机的 Host 名称
            string hostName = Dns.GetHostName();
            // 获取本机的 IP 地址列表
            IPAddress[] addresses = Dns.GetHostEntry(hostName).AddressList;

            StringBuilder sb = new StringBuilder();
            foreach (IPAddress address in addresses)
            {
                sb = sb.Append(address.ToString() + "\r\n");
            }

            txtIPs.Text = sb.ToString();

            App.UDIEvent += App_UDIEvent;
        }

        private void App_UDIEvent(UDICode code)
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (code != null)
                {
                    txtDi.Text = code.Di;
                    txtBN.Text = code.BatchNumber;
                    txtPD.Text = code.ProductionDate;
                    txtED.Text = code.ExpirationDate;
                    txtSN.Text = code.SerialNumber;

                    string sendData = JsonConvert.SerializeObject(new List<UDICode> { code }, Formatting.None);
                    UDI.KeyOutPut(sendData);
                }
                else
                {
                    txtDi.Text =
                    txtBN.Text =
                    txtPD.Text =
                    txtED.Text =
                    txtSN.Text = null;
                }
            });
        }
    }
}